import React, { lazy } from "react"
import { createBrowserRouter } from "react-router-dom"

// import MainLayout from "../pages/layouts/MainLayout"
// import ManageLayout from "../pages/layouts/ManageLayout"
// import QuestionLayout from "../pages/layouts/QuestionLayout"
// import Home from "../pages/Home"
// import Login from "../pages/Login"
// import Register from "../pages/Register"
// import NotFound from "../pages/NotFound"
// import List from "../pages/manage/List"
// import Trash from "../pages/manage/Trash"
// import Star from "../pages/manage/Star"
// import Edit from "../pages/question/Edit"
// import Stat from "../pages/question/Stat"

// 路由懒加载
// 加注释是为了build方便查看
const MainLayout = lazy(() => import(/* webpackChunkName:"MainLayout" */ "../pages/layouts/MainLayout"))
const ManageLayout = lazy(() => import(/* webpackChunkName:"ManageLayout" */ "../pages/layouts/ManageLayout"))
const QuestionLayout = lazy(() => import(/* webpackChunkName:"QuestionLayout" */ "../pages/layouts/QuestionLayout"))
const Home = lazy(() => import(/* webpackChunkName:"Home" */ "../pages/Home"))
const Login = lazy(() => import(/* webpackChunkName:"Login" */ "../pages/Login"))
const Register = lazy(() => import(/* webpackChunkName:"Register" */ "../pages/Register"))
const NotFound = lazy(() => import(/* webpackChunkName:"NotFound" */ "../pages/NotFound"))
const List = lazy(() => import(/* webpackChunkName:"List" */ "../pages/manage/List"))
const Trash = lazy(() => import(/* webpackChunkName:"Trash" */ "../pages/manage/Trash"))
const Star = lazy(() => import(/* webpackChunkName:"Star" */ "../pages/manage/Star"))
const Edit = lazy(() => import(/* webpackChunkName:"Edit" */ "../pages/question/Edit"))
const Stat = lazy(() => import(/* webpackChunkName:"Stat" */ "../pages/question/Stat"))

const router = createBrowserRouter([
    {
        path: "/",
        element: <MainLayout />,
        children: [
            {
                path: "/",
                element: <Home />
            },
            {
                path: "/login",
                element: <Login />
            },
            {
                path: "/register",
                element: <Register />
            },
            {
                path: "/manage",
                element: <ManageLayout />,
                children: [
                    {
                        path: "/manage/list",
                        element: <List />
                    },
                    {
                        path: "/manage/star",
                        element: <Star />
                    },
                    {
                        path: "/manage/trash",
                        element: <Trash />
                    }
                ]
            },
            {
                path: "*", // 404路由配置
                element: <NotFound />
            }
        ]
    },
    {
        path: "/question",
        element: <QuestionLayout />,
        children: [
            {
                path: "/question/edit/:id",
                element: <Edit />
            },
            {
                path: "/question/stat/:id",
                element: <Stat />
            }
        ]
    }
])

export default router

// -------- 分割线 ------------
export const HOME_PATHNAME = "/"
export const LOGIN_PATHNAME = "/login"
export const REGISTER_PATHNAME = "/register"
export const MANGE_INDEX_PATHNAME = "/manage/list"

export function isLoginOrRegister(pathname: string) {
    // 如果登录页和注册页  包含了这个路由就返回true
    if ([LOGIN_PATHNAME, REGISTER_PATHNAME].includes(pathname)) return true
    return false
}

// 判断页面是不是不需要用户信息
export function isNoNeedUserInfo(pathname: string) {
    // 首页、注册页、登录页不需要用户信息
    if ([HOME_PATHNAME, LOGIN_PATHNAME, REGISTER_PATHNAME].includes(pathname)) return true
    return false
}
